# Go参数
GOCMD=go
GOBUILD=$(GOCMD) build
GOTEST=$(GOCMD) test
GOCLEAN=$(GOCMD) clean
GOMOD=$(GOCMD) mod
GOGET=$(GOCMD) get
GOVET=$(GOCMD) vet

# 项目参数
BINARY_NAME=deployer
BINARY_DIR=bin
MAIN_FILE=cmd/deployer/main.go
VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME=$(shell date "+%Y-%m-%d %H:%M:%S")
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")

# 编译参数
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)"

# 测试参数
COVERAGE_DIR=coverage
COVERAGE_FILE=$(COVERAGE_DIR)/coverage.out
COVERAGE_HTML=$(COVERAGE_DIR)/coverage.html
COVERAGE_THRESHOLD=80
TEST_TIMEOUT=5m
TEST_FLAGS=-race -v -count=1

# 清理编译产物
.PHONY: clean
clean:
	@echo "清理编译产物..."
	@rm -rf $(BINARY_DIR)
	@rm -rf $(COVERAGE_DIR)
	@$(GOCLEAN)
	@echo "清理完成"

# 安装依赖
.PHONY: deps
deps:
	@echo "安装依赖..."
	@$(GOMOD) download
	@echo "依赖安装完成"

# 代码格式化
.PHONY: fmt
fmt:
	@echo "格式化代码..."
	@gofmt -w .
	@echo "格式化完成"

# 代码检查
.PHONY: vet
vet:
	@echo "检查代码..."
	@$(GOVET) ./...
	@echo "检查完成"

# 运行测试
.PHONY: test
test:
	@echo "运行测试..."
	@$(GOTEST) $(TEST_FLAGS) -timeout $(TEST_TIMEOUT) ./...
	@echo "测试完成"

# 生成测试覆盖率报告
.PHONY: coverage
coverage:
	@echo "生成测试覆盖率报告..."
	@mkdir -p $(COVERAGE_DIR)
	@$(GOTEST) $(TEST_FLAGS) -coverprofile=$(COVERAGE_FILE) -covermode=atomic ./...
	@$(GOCMD) tool cover -html=$(COVERAGE_FILE) -o $(COVERAGE_HTML)
	@echo "覆盖率报告生成完成: $(COVERAGE_HTML)"

# 检查测试覆盖率
.PHONY: check-coverage
check-coverage: coverage
	@echo "检查测试覆盖率..."
	@coverage=$$(go tool cover -func=$(COVERAGE_FILE) | grep total | grep -Eo '[0-9]+\.[0-9]+'); \
	if [ $$(echo "$$coverage < $(COVERAGE_THRESHOLD)" | bc -l) -eq 1 ]; then \
		echo "测试覆盖率 $$coverage% 低于目标 $(COVERAGE_THRESHOLD)%"; \
		exit 1; \
	else \
		echo "测试覆盖率 $$coverage% 达到目标 $(COVERAGE_THRESHOLD)%"; \
	fi

# 构建项目（完整流程）
.PHONY: build
build: clean deps fmt vet test
	@echo "构建项目..."
	@mkdir -p $(BINARY_DIR)
	@$(GOBUILD) $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME) $(MAIN_FILE)
	@echo "构建完成: $(BINARY_DIR)/$(BINARY_NAME)"

# 快速构建（跳过依赖安装和代码检查）
.PHONY: build-fast
build-fast:
	@echo "快速构建项目..."
	@mkdir -p $(BINARY_DIR)
	@$(GOBUILD) $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME) $(MAIN_FILE)
	@echo "构建完成: $(BINARY_DIR)/$(BINARY_NAME)"

# 本地构建（使用构建脚本）
.PHONY: build-local
build-local:
	@echo "执行本地构建..."
	@chmod +x scripts/build.sh
	@./scripts/build.sh

# 安装到系统
.PHONY: install
install: build
	@echo "安装到系统..."
	@cp $(BINARY_DIR)/$(BINARY_NAME) /usr/local/bin/
	@echo "安装完成"

# 卸载
.PHONY: uninstall
uninstall:
	@echo "卸载程序..."
	@rm -f /usr/local/bin/$(BINARY_NAME)
	@echo "卸载完成"

# 运行集成测试
.PHONY: integration-test
integration-test:
	@echo "运行集成测试..."
	@INTEGRATION_TEST=true $(GOTEST) $(TEST_FLAGS) -tags=integration -timeout $(TEST_TIMEOUT) ./...
	@echo "集成测试完成"

# 帮助信息
.PHONY: help
help:
	@echo "可用的 make 命令："
	@echo "  make clean            - 清理编译产物"
	@echo "  make deps             - 安装依赖"
	@echo "  make fmt              - 格式化代码"
	@echo "  make vet              - 代码检查"
	@echo "  make test             - 运行测试"
	@echo "  make coverage         - 生成测试覆盖率报告"
	@echo "  make check-coverage   - 检查测试覆盖率是否达标"
	@echo "  make build            - 构建项目（完整流程）"
	@echo "  make build-fast       - 快速构建项目"
	@echo "  make build-local      - 本地构建（使用构建脚本）"
	@echo "  make install          - 安装到系统"
	@echo "  make uninstall        - 从系统卸载"
	@echo "  make integration-test - 运行集成测试"

# 默认目标
.DEFAULT_GOAL := build
